/*
 * Copyright (C) 2008 Jens Meiß
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.googlecode.hmvc4gwt.example.hmvcblog.server.repository.user;

import java.util.List;


import javax.persistence.EntityExistsException;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;

import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.hmvc4gwt.example.hmvcblog.server.domain.user.User;
import com.googlecode.hmvc4gwt.example.hmvcblog.server.repository.user.exceptions.UserExistException;
import com.googlecode.hmvc4gwt.example.hmvcblog.server.repository.user.exceptions.UserNotFoundException;
import com.googlecode.hmvc4gwt.example.hmvcblog.server.repository.user.exceptions.UserSessionNotFoundException;

/**
 * DAO to access the user data
 * 
 * @author jens.meiss
 */
@Transactional
public class UserRepository extends JpaDaoSupport implements IUserRepository {

	public User findById(Integer id) throws UserNotFoundException {

		User user = getJpaTemplate().find(User.class, id);

		if (user == null) {
			throw new UserNotFoundException();
		}

		return user;
	}

	public void removeUser(User user) throws UserNotFoundException {

		getJpaTemplate().remove(user);

	}

	public void storeUser(User user) throws UserExistException {

		try {

			getJpaTemplate().merge(user);

		} catch (EntityExistsException e) {
			
			throw new UserExistException();
			
		} 
	}

	
	public User findByLogin(String login) throws UserNotFoundException {

		try {
			@SuppressWarnings("unchecked")
			List<User> list = getJpaTemplate().findByNamedQuery(
					"User.findByLogin", login);

			if (list.size() == 1) {
				return list.get(0);
			} else {
				throw new UserNotFoundException();
			}

		} catch (NonUniqueResultException e) {

			throw new UserNotFoundException();

		} catch (IllegalStateException e) {

			throw new UserNotFoundException();

		}

	}
	
	
	public User findBySessionId(Long sessionId)
			throws UserSessionNotFoundException {

		try {
			
			@SuppressWarnings("unchecked")
			List<User> listUser = getJpaTemplate().findByNamedQuery("User.findBySessionId", sessionId);
			
			if (listUser.size() == 1) {
				return listUser.get(0);
			} else {
				throw new UserSessionNotFoundException();
			}

		} catch (NoResultException e) {
			throw new UserSessionNotFoundException();
		} catch (Exception e) {
			throw new UserSessionNotFoundException();
		}
	}

	@Override
	public void updateUser(User user) {
		
		getJpaTemplate().merge(user);
		getJpaTemplate().flush();
		
	}
}
